package com.snow.fvmusic.controller;

import com.snow.fvmusic.bo.CommentAndGreatParam;
import com.snow.fvmusic.bo.CommonResult;
import com.snow.fvmusic.generator.entities.CommentMessage;
import com.snow.fvmusic.generator.entities.GreatMessage;
import com.snow.fvmusic.generator.service.CommentMessageService;
import com.snow.fvmusic.generator.service.GreatMessageService;
import com.snow.fvmusic.service.MessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author ：snow
 * @Date ：Created in 2020-10-21
 * @Description：
 * @Modified By：
 * @Version:
 */
@RestController
@Api("用户消息")
@RequestMapping("/message")
public class MessageController {
    @Resource
    private MessageService messageService;
    @Resource
    private GreatMessageService greatMessageService;
    @Resource
    private CommentMessageService commentMessageService;
    @PostMapping("/comment")
    @ApiOperation("获取评论消息")
    public CommonResult getCommentMessage(
            @RequestParam(name="userId", required = true) Long userId,
            @RequestParam(value = "limit",required = false, defaultValue = "50") Integer limit){
        List<CommentAndGreatParam> res = messageService.getCommentMessage(userId, limit);
        return CommonResult.success(res);
    }

    @PostMapping("/great")
    @ApiOperation("获取点赞消息")
    public CommonResult getGreatMessage(
            @RequestParam(name="userId", required = true) Long userId,
            @RequestParam(name="limit", required = false, defaultValue = "50") Integer limit
    ){
        return CommonResult.success(messageService.getGreatMessage(userId, limit));
    }
    @PostMapping("/hasRead")
    @ApiOperation(value = "标记消息已读", notes = "0 ---> great message; 1---->comment message")
    public void handleMessageHashRead(
      @RequestParam(name="messageId", required = true) Long messageId,
      @RequestParam(name="greatOrComment") Integer greatOrComment,
      HttpServletResponse resp
    ){
        if (greatOrComment == 0){
            GreatMessage gm = new GreatMessage();
            gm.setHasRead(true);
            gm.setId(messageId);
            greatMessageService.updateById(gm);
        }else if (greatOrComment == 1){
            CommentMessage cm = new CommentMessage();
            cm.setId(messageId);
            cm.setHasRead(true);
            commentMessageService.updateById(cm);
        }else{
            try {
                resp.getWriter().println("参数greatOrComment 只能为0 或 1");
                resp.setStatus(440);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
